<?php

class PhotoCategory extends AppModel {
    var $name = 'PhotoCategory';
    var $virtualFields = array(
        'start_timestamp' => "'0'",
        'stop_timestamp' => "'0'"
    );
    var $hasMany = array("Photo" => array('foreignKey' => 'category_id'));
    
    public function afterFind($results, $primary) {
        if(!empty($results[0]['PhotoCategory']) && !empty($results[0]['Photo'][0])) {
            foreach($results as $num => $data) {
                $min = PHP_INT_MAX;
                $max = 0;
                
                foreach($data['Photo'] as $photo) {
                    if($photo['timestamp'] > $max) $max = $photo['timestamp'];
                    if($photo['timestamp'] < $min) $min = $photo['timestamp'];
                }
                
                $results[$num]['PhotoCategory']['start_timestamp'] = $min;
                $results[$num]['PhotoCategory']['stop_timestamp'] = $max;
            } 
        }
        return $results;
    }
}
?>
